﻿using GZNest.Biz.IService;
using GZNest.Biz.Service.Spin.System;
using GZNest.Entity.Spin.System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace GZNest.Web.Areas.Spin.Controllers
{
    public class MachineController : Controller
    {
        private IMachineService machineService;
        public MachineController()
        {
            machineService = new MachineService();
        }

        // GET: SpinSystem
        public ActionResult Index()
        {
            var machines = machineService.FindAll();
            return View(machines);
            //return Json(machines, JsonRequestBehavior.AllowGet);
        }

        // GET: SpinSystem/Details/5
        public ActionResult Details(int id)
        {
            Machine m = machineService.Find(id);
            if (m == null)
            {
                return HttpNotFound();
            }
            return View(m);
        }

        // GET: SpinSystem/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: SpinSystem/Create
        [HttpPost]
        public ActionResult Create(Machine newMachine)
        {
            try
            {
                // TODO: Add insert logic here
                if (ModelState.IsValid)
                {
                    machineService.Add(newMachine);
                    return RedirectToAction("Index");
                }
                else
                {
                    return View(newMachine);
                }
            }
            catch
            {
                return View();
            }
        }

        // GET: SpinSystem/Edit/5
        public ActionResult Edit(int id)
        {
            Machine m = machineService.Find(id);
            if (m == null)
            {
                return HttpNotFound();
            }
            return View(m);
        }

        // POST: SpinSystem/Edit/5
        [HttpPost]
        public ActionResult Edit(Machine machine)
        {
            try
            {
                // TODO: Add update logic here
                if (ModelState.IsValid)
                {
                    machineService.Update(machine);
                    return RedirectToAction("Index");
                }
                else
                {
                    return View(machine);
                }
            }
            catch
            {
                return View();
            }
        }

        // GET: SpinSystem/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        // POST: SpinSystem/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }


        public ActionResult List()
        {
            return View();
        }

        public JsonResult GetAllMachines()
        {
            var machines = machineService.FindAll();
            return Json(machines, JsonRequestBehavior.AllowGet);
        }

        public JsonResult AddMachine(Machine item)
        {
            item = machineService.Add(item);
            return Json(item, JsonRequestBehavior.AllowGet);
        }

        public JsonResult UpdateMachine(Machine machine)
        { 
            if (machineService.Update(machine))
            {
                return Json(machineService.FindAll(), JsonRequestBehavior.AllowGet);
            }

            return Json(null);
        }

        public JsonResult DeleteMachine(Machine machine)
        {
            if (machineService.Delete(machine))
            {
                return Json(new { Status = true }, JsonRequestBehavior.AllowGet);
            }

            return Json(new { Status = false }, JsonRequestBehavior.AllowGet);
        }

    }
}
